/******************************************************************
Copyright © LibKz 2017~2023. All rights reserved.
* @projectName   LibKz
* @author        Raiiwhen
* @email         masterraii@icloud.com
* @origin        05_Console\00_KingsHand\KingsHandVerX\Drv\MeX_TXer\Src
*******************************************************************/
#include <stdio.h>
#include "TX.h"
#include <string.h>
#include "dev.h"

extern SPI_HandleTypeDef hspi1;
extern dev_RF txer;
extern dev_ADC adc128s022;

static uint8_t ADC_TX[16] = {0x00, 0x00, 0x08, 0x00, 0x10, 0x00, 0x18, 0x00, 0x20, 0x00, 0x28, 0x00, 0x30, 0x00, 0x39, 0x00};
static uint8_t ADC_RX[16] = {0};

dev_SBUS SBUS_Handle;

uint8_t ADC_Update(uint16_t* buf){
    SPI1_CS2 = 0;
    HAL_SPI_TransmitReceive(&hspi1, ADC_TX, ADC_RX, 16, 9);
    for (int i = 0; i < 8;i++){
        buf[i] = ADC_RX[2 * i] << 8 | ADC_RX[2 * i + 1];

        /*from unknow reason*/
        buf[i] *= 2;
    }
    SPI1_CS2 = 1;

    return 0;
}

uint8_t SBUS_Init(){

    return 1;
}

uint8_t nRF_Pkg_Update(void){
    //ADC Channel
    memcpy(txer.pkg.ch, adc128s022.ch, 16);

    //Switch Channel
    memset(txer.pkg.sw , 0x55, 4);

    //And config
    memset(txer.pkg.cfg, 0xff, 10);

    //CRR
    uint8_t *sum_ptr = (uint8_t*)(&txer.pkg);
		txer.pkg.sum = 0;
    for (uint8_t cnt = 0; cnt < 31;cnt++){
        txer.pkg.sum += *(sum_ptr + cnt);
    }

    return 0;
}
